package engine.newUi
{
	import engine.data.BindManage;
	import engine.data.ModelLocator;
	import engine.data.ResPath;
	import engine.data.RoleVo;
	import engine.flag.ResourcesPath;
	import engine.tip.TipsMainLogic;
	import engine.ui.lite.BaseUI;
	
	import flash.display.DisplayObject;
	import flash.display.DisplayObjectContainer;
	import flash.display.MovieClip;
	import flash.events.Event;
	import flash.events.EventDispatcher;
	import flash.events.MouseEvent;
	
	import newx.com.control.window.NxWindowEvent;
	import newx.utils.MemoryOptimise;

	public class RiverLakeLogic
	{
		private static var _self:RiverLakeLogic;
		private const BTN_BEF:String="a1";
		private const BUILD_BEF:String="a";
		
		public static function getInstance():RiverLakeLogic
		{
			if(_self==null)
			{
				_self=new RiverLakeLogic();
			}
			return _self;
		}
		private var _ui:RiverLakeUI;
		private var OPEN_UI:Object=new Object;
		private var TIPS_STR:Object=new Object;
		public function get ui():RiverLakeUI
		{
			if(_ui==null)
			{
				_ui=new RiverLakeUI
			}
			return _ui;
		}
		public function RiverLakeLogic()
		{
			
		}
		private var _bind:BindManage;
		private var _model:ModelLocator
		private var _role:RoleVo;
		private var _tip:TipsMainLogic;
		
		private var _rollMc:MovieClip

		public function set rollMc(value:MovieClip):void
		{
			if(_rollMc)
			{
				if(_rollMc.totalFrames==4)
				{
					_rollMc.gotoAndStop(1);
				}
				else
				{
					_rollMc.gotoAndStop(1);
				}
			}
			_rollMc = value;
			if(value)
			{
				if(value.totalFrames==4)
				{
					value.gotoAndStop(2);
				}
				else
				{
					value.gotoAndStop(2);
				}
			}
		}
		public function  Init():void
		{
			_ui.layout	=	BaseUI.CENTER;
			_bind=BindManage.getInstance();
			_model=ModelLocator.getInstance();
			_role=_model.scene.Role;
			_tip=TipsMainLogic.instance;
			_ui.mainClip.source=ResPath.SWF+"RiverLake.swf";
			_ui.mainClip.addLoadedEvent(onLoadImg,Event.COMPLETE);
			initUIOpen();
			initTips();
			
		}
		private function initUIOpen():void
		{
			OPEN_UI["a11"]=FriendUI.getInstance().showOrHide;
			OPEN_UI["a13"]=JHtravelLogic.getInstance().ui.showOrHide;
			OPEN_UI["a14"]=DigCimeliaLogic.getInstance().ui.showOrHide;
			OPEN_UI["a15"]=function (obj:*=null):void{NotepadLogic.getInstence().ui.showOrHide(obj);}
			OPEN_UI["a16"]=BanksUI.getInstance().showOrHide;
			OPEN_UI["a17"]=close;
			OPEN_UI["a18"];
		}
		private function initTips():void
		{
			TIPS_STR["a13"]=DailyWorkLogic.getInstance().ui.showOrHide;
			TIPS_STR["a14"]=DigCimeliaLogic.getInstance().ui.showOrHide;
			TIPS_STR["a15"]//=NotepadLogic.getInstence().ui.show;
			TIPS_STR["a16"]=BanksUI.getInstance().showOrHide;
			TIPS_STR["a17"]=close;
		}
		private function onLoadImg(val:DisplayObjectContainer):void
		{
			MemoryOptimise.stopMovie(val);
		}
		public function	BeforeShow():void
		{
			addAllEventListener()
		}
		public function	AfterHide():void
		{
			removeAllEventListener()
		}
		private function upBtnHandle(e:Event):void
		{
			var dp:DisplayObject=e.target as MovieClip;
			if(dp==null)return ;
			
			var str:String=findBtn(dp);
			str	||=	findBuild(dp);
//			trace("RiverLakeLogic.clickBtnHandle(e)当前点击",str);
			if(str == "a1")
			{
				showUI(EscortSceneUI.getInstance(),_ui)
			}
			var mc:MovieClip=getMc(str);
			if(mc.totalFrames==4&&mc.currentFrame==3)
			{
				mc.gotoAndStop(2);
				if(OPEN_UI.hasOwnProperty(str))
					OPEN_UI[str](_ui);
				else
//					trace("Error~RiverLakeLogic.clickBtnHandle(e)",str);
			}
			else if(mc.totalFrames==2&&mc.currentFrame==1)
			{
				mc.gotoAndStop(2);
				if(OPEN_UI.hasOwnProperty(str))
					OPEN_UI[str](_ui);
				else
//					trace("Error~RiverLakeLogic.clickBtnHandle(e)",str);
			}
			
		}
		private function clickBtnHandle(e:Event):void
		{
			var dp:DisplayObject=e.target as MovieClip;
			if(dp==null)return ;
				
			var str:String=findBtn(dp);
			str	||=	findBuild(dp);
			var mc:MovieClip=getMc(str);
			if(mc.totalFrames==4)
			{
				mc.gotoAndStop(3);
			}
			else
			{
				mc.gotoAndStop(1);
			}
		}
		public function set upIndustry(industryID:int):void
		{
			var mc:MovieClip=getMc(BUILD_BEF+industryID);
			if(mc)
				mc.gotoAndStop(1)
			else
//				trace("RiverLakeLogic.setUpIndustry(industryID)没有这个建筑");
			
		}
		public function set closeIndustry(industryID:int):void
		{
			var mc:MovieClip=getMc(BUILD_BEF+industryID);
			if(mc)
				mc.gotoAndStop(4)
			else
//				trace("RiverLakeLogic.setUpIndustry(industryID)没有这个建筑");
		}
		private function rollOnHandle(e:Event):void
		{
			var dp:DisplayObject=e.target as MovieClip;
			if(dp==null)return ;
			var str:String=findBtn(dp);
			str	||=	findBuild(dp);
			var mc:MovieClip=getMc(str);
			var tips:String="";
			rollMc=mc;
			tips=str;
			if(tips)
				_tip.showMain(tips);
				
		}
		private function rollOutHandle(e:Event):void
		{
			rollMc=null;
			_tip.hide();
		}
		private function findBuild(dp:DisplayObject):String
		{
			return ransackAllMc(dp,BUILD_BEF);
		}
		private function findBtn(dp:DisplayObject):String
		{
			return ransackAllMc(dp,BTN_BEF);
		}
		private function getMc(str:String):MovieClip
		{
			return _ui.mainClip.content[str];
		}
		private function ransackAllMc(dp:DisplayObject,bef:String):String
		{
			var i:int=1;
			var mc:MovieClip;
			while(getMc(bef+i))
			{
				mc=getMc(bef+i);
				if(mc.contains(dp))
				{
					return bef+i
				}
				i++
			}
			return "";
		}
		private function sackAllMc(bef:String,func:Function):void
		{
			var i:int=1;
			var mc:MovieClip;
			while(getMc(bef+i))
			{
				mc=getMc(bef+i);
				func(mc);
				i++;
			}
		}
		public function addAllEventListener():void
		{
			var $this:Object=this;
			_ui.addEventListener(NxWindowEvent.WINDOW_CLOSE,close);
			_ui.mainClip.content.addEventListener(MouseEvent.MOUSE_DOWN,clickBtnHandle)
			_ui.mainClip.content.addEventListener(MouseEvent.MOUSE_UP,upBtnHandle)
			_ui.mainClip.content.addEventListener(MouseEvent.MOUSE_OVER,rollOnHandle);
			_ui.mainClip.content.addEventListener(MouseEvent.MOUSE_OUT,	rollOutHandle)
			sackAllMc(BUILD_BEF,function (mc:MovieClip):void{
				mc.mouseChildren=false;
			})
		}
		public function removeAllEventListener():void
		{
			_ui.removeEventListener(NxWindowEvent.WINDOW_CLOSE,close)
			_ui.mainClip.content.removeEventListener(MouseEvent.MOUSE_DOWN,clickBtnHandle)
			_ui.mainClip.content.removeEventListener(MouseEvent.MOUSE_UP,upBtnHandle)
			_ui.mainClip.content.removeEventListener(MouseEvent.MOUSE_OVER,rollOnHandle)
			_ui.mainClip.content.removeEventListener(MouseEvent.MOUSE_OUT,rollOutHandle)
		}
		private function close(...args):void
		{
			_ui.hide();
		}
		private function showUI(ui : *, parent : DisplayObjectContainer):void
		{
			ui.mouseChildren	=	ui.mouseEnabled	=	ui.visible	=	true;
			parent.addChild(ui);
			
			if(ui is BaseUI)
			{
				ui.x	=	ui.initPosX;
				ui.y	=	ui.initPosY;
				
				( ui as BaseUI ).beforeShow();
			}
		}
	}
}